<template>
  <div class="parent">
    <echartsDom v-if="isShowEcharts" style="width: 100%; height: 100%" :width="width" :height="height" :option="option" :type="type" />
    <div v-else>
      <px-empty :image-size="200" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, toRefs, ref, watch } from "vue";
import echartsDom from "./echartsDom.vue";
const props = defineProps({
  width: {
    type: String,
    default: ""
  },
  height: {
    type: String,
    default: ""
  },
  chartlist: {
    type: Object,
    required: true
  }
});

const isShowEcharts = ref(false);
const state = reactive({
  option: {},
  series: [],
  yAxis: []
});
const { option, series, yAxis } = toRefs(state);

const getoptions = () => {
  series.value = [];
  yAxis.value = [];
  console.log(props.chartlist.length);
  if (props.chartlist.length > 0) {
    isShowEcharts.value = true;
  } else {
    isShowEcharts.value = false;
  }
  option.value = {
    toolbox: {
      show: true,
      feature: {
        saveAsImage: {
          title: "点击保存为图片",
          name: "历史记录"
        }
      }
    },
    grid: {
      left: "40px",
      top: "50px",
      right: "50px",
      bottom: "0px",
      containLabel: true
    },
    legend: {
      top: "1%",
      type: "scroll",
      width: "1000px"
    },
    tooltip: {
      confine: true,
      trigger: "axis"
    },
    xAxis: {
      type: "category",
      boundarGap: false,
      data: props.chartlist.map(item => item.ctime)
    },
    yAxis: {
      type: "value"
    },
    series: [
      {
        // name: item.name,
        type: "line",
        data: props.chartlist.map(item => item.value),
        label: {
          show: true,
          position: "top"
        }
      }
    ]
  };
};
watch(
  () => props.chartlist,
  newOption => {
    console.log(newOption);
    getoptions();
  },
  { immediate: true }
);
</script>

<style scoped lang="scss">
.parent {
  position: relative;
  float: left;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.topIcon {
  position: absolute;
  top: 7px;
  right: 8px;
  width: 20px;
  cursor: pointer;
}

.linediv {
  position: relative;
  float: left;
  width: 23px;
  height: 17px;
  margin-right: 10px;

  .line {
    position: absolute;
    top: 8px;
    width: 100%;
    height: 1px;
    background: #3b82f6;
  }

  .yuan {
    position: absolute;
    top: 6px;
    left: 9px;
    width: 5px;
    height: 5px;
    background: #3b82f6;
    border-radius: 50%;
  }
}
</style>
